+(function($) {

    if (social) {
        if (social.github) {
            appendSocial(social.github, 'icon fa fa-github');
        }
        if (social.oschina) {
            appendSocial(social.oschina, 'icon icon-opera');
        }
        if (social.linkedin) {
            appendSocial(social.linkedin, 'icon fa fa-linkedin');
        }
        if (social.weibo) {
            appendSocial(social.weibo, 'icon icon-weibo');
        }
        if (social.mail) {
            appendSocial(social.mail, 'icon icon icon-envelope');
        }
    }

    if (qrcode && qrcode.alipay) {
        $('.qrc-alipay').attr('src', qrcode.alipay);
    }

    $("#search-field").ghostHunter({
        results: "#results",
        info_template: '<small class="text-muted">找到 {{amount}} 条记录：</small>',
        result_template: '<a href="{{link}}"><h4>{{title}}</h4><p><small>{{description}}</small></p><p><small>Tags: {{category}}</small><small class="pull-right">{{pubDate}}</small</p><hr></a>'
    });
    $('.post-toc .panel-body').toc({
        selectors: "h1,h2,h3,h4,h5,h6",
        container: "article .content"

    });
    $(window).scroll(function() {
        document.documentElement.scrollTop + document.body.scrollTop > 80 ?
            $('.backTop').fadeIn() :
            $('.backTop').fadeOut();

        document.documentElement.scrollTop + document.body.scrollTop > 250 ?
            $('.post-toc').addClass("posttoc-fixed") :
            $('.post-toc').removeClass("posttoc-fixed");
    });

    $(".backTop").on("click", function() {
        scrollToTop();
    });

    $("html,#searchModal section").niceScroll({
        cursorcolor: "#ccc"
    });

    var urlKit = {
        domain : function(_url){
            return _url?urlKit.pure(_url).replace(/https|http|ftp|rtsp|mms|:|\//g,''):document.domain;
        },
        hostname : function(){
            return location.hostname;
        },
        pathname : function(){
            return location.pathname;
        },
        port : function(){
            return location.port;
        },
        protocol : function(){
            return location.protocol;
        },
        pure : function(_url){
            var match = _url.match(/((https|http|ftp|rtsp|mms):\/\/)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)/g);
            if(match && match.length > 0){
                return match[0];
            }
            return urlKit.domain();
        }
    }
    
    $('a').each(function(i,e){
        if(urlKit.domain()!= urlKit.domain($(e).attr('href')) && $(e).attr('target')!='_blank'){
            $(e).attr('target','_blank');
        }
    });

    function loadImg(dom, imgUrl) {
        dom.css('background-image', 'url(' + imgUrl + ')').css('background-position', 'center').css('background-repeat', 'no-repeat').css('background-size', 'contain');
    }

    function appendSocial(url, icon) {
        $('.blog-info .panel-body').append('<a class="social-logo" href="' + url + '" target="_blank"><i class="' + icon + '"></i></a>');
    }

    function scrollToTop(dom, speed) {
        if (!speed) speed = 300;
        var top = 0;
        if (dom && dom.length > 0) top = dom.offset().top;
        $('html,body').animate({
            scrollTop: top
        }, speed)

    }
})
(jQuery);